package zmee.pmas.form;

import dm.jdbc.util.StringUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.list.BillList;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author: zyj
 * @Date: 2023/9/1 20:12
 * @Description: 项目信息调整: 点击更新原项目概算按钮时 查询 项目支出预算 更新 原项目概算
 */
public class ProApprovalUpdateProPlanAmtListPlugin  extends AbstractListPlugin {

    private static final String BUTTON_KEY_UPDATEOLDPROPLANAMT = "zmee_updateoldproplanamt";

    @Override
    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        // 点击更新原项目概算按钮时
        if (StringUtil.equalsIgnoreCase(BUTTON_KEY_UPDATEOLDPROPLANAMT, evt.getItemKey())) {
            BillList list = this.getControl(BILLLISTID);
            ListSelectedRowCollection coll = list.getSelectedRows();
            // 判断是否选中行
            if (coll == null || coll.size() <= 0) {
                this.getView().showTipNotification("请至少选择一行数据！");

            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        if (StringUtil.equalsIgnoreCase(BUTTON_KEY_UPDATEOLDPROPLANAMT, evt.getItemKey())) {
            BillList list = this.getControl(BILLLISTID);
            String billFormId = list.getBillFormId();
            ListSelectedRowCollection selectedRows = list.getSelectedRows();
            ArrayList<DynamicObject> dynamicObjects = new ArrayList();
            for (ListSelectedRow selectedRow : selectedRows) {
                Object primaryKeyValue = selectedRow.getPrimaryKeyValue();
                // 获取原表单数据
                DynamicObject dynamicObjectObj = BusinessDataServiceHelper.loadSingle(primaryKeyValue, billFormId);
                // 获取项目
                DynamicObject project = dynamicObjectObj.getDynamicObject("pro");
                // 根据项目和有效查询项目支出预算
                QFilter qFilter = new QFilter("project.number", "=", project.getString("number"));
                QFilter qFilter1 = new QFilter("isvalid", "=", true);
                DynamicObject[] outbudgets = BusinessDataServiceHelper.load("pmas_outbudget", "id, zmee_textfield", new QFilter[]{qFilter, qFilter1});
                if (outbudgets != null && outbudgets.length != 0) {
                    BigDecimal projectPlanAmount = outbudgets[0].getBigDecimal("zmee_textfield");
                    dynamicObjectObj.set("zmee_oldprojectplanamt", projectPlanAmount);
                    dynamicObjects.add(dynamicObjectObj);
                }
            }
            SaveServiceHelper.save(dynamicObjects.toArray(new DynamicObject[0]));
        }
    }

}
